<template>
  <div class="input-wrapper">
    <input :type="type" :placeholder="placeholder" class="custom-input" @input="handleInput" :value="modelValue" @focus="boxState = 'focus',$emit('focus', $event)" @blur="boxState = 'blur',$emit('blur', $event)" />
    <div class="p1757298071 fac">
      <div class="p1757298694 f">
        <uv-icon v-if="modelValue " @click="clearInput" size="30rpx" name="close-circle-fill"></uv-icon>
      </div>
      <div class="p1757297333 ">
        <slot></slot>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: 'InputWithClear',
  props: {
    // 输入框类型，默认为text
    type: {
      type: String,
      default: 'text'
    },
    // 输入框占位符
    placeholder: {
      type: String,
      default: ''
    },
    // 输入框值，用于v-model绑定
    modelValue: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      timer: null,
      clientWidth: 0,
      boxState: ''
    };
  },

  mounted() {
  },
  methods: {
    handleInput(e) {
      const value = e.detail.value
      // 触发更新事件，父组件通过v-model接收
      this.$emit('update:modelValue', value)
      // 同时触发input事件，兼容传统用法
      this.$emit('input', value)
    },
    clearInput() {
      this.$emit('update:modelValue', '')
      this.$emit('clear'); // 触发清除事件，供父组件使用
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../common.scss";
.input-wrapper {
  display: inline-block;
  padding-left: 30rpx;
  box-sizing: border-box;
  background: #ffffff;
  border-radius: 32rpx;
  width: 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;

  .p1757297333 {
    padding-left: 15rpx;
    height: 100%;
    line-height: 1;
  }

  .p1757298071 {
    background: transparent;
    height: 100%;
  }
}

.custom-input {
  width: 100%;
  height: 100%;
  border: none;
  border-radius: 8rpx;
  font-size: 30rpx !important;
  box-sizing: border-box;
  transition: border-color 0.2s;
  padding: 0 20rpx 0 0rpx;
}

.custom-input:focus {
  outline: none;
}

.clear-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: color 0.2s;
}

.clear-btn:hover {
  color: #333;
}

.clear-btn svg {
  pointer-events: none;
}

.p1757298694 {
  height: 100%;
  align-items: center;

  padding-top: 5rpx;
}
</style>